const Controller = require("uni-cloud-router").Controller;
const {validateToken} = require('validdate-token');
module.exports = class UserController extends Controller{
	/**
	 * @param {Object} token
	 * 验证token
	 */
	async _checkToken(token){
		let tokenRes;
		try{
			tokenRes = await validateToken(token);
			if(tokenRes.code !== 0){
				return{
					status:-1,
					msg:"稍后重试",
					errCode:tokenRes.errCode
				}
			} else {
				return {
					status:0,
					msg:"token有效",
				}
			}
		}catch(e){
			return {
			  status: -1,
			  errCode: 'TOKEN_INVALID',
			  msg: 'token无效'
			}
		}
	}
	/**
	 * 2020-11-17
	 * 给项目分配员工
	 */
	async allotWorkerInProject(){
		let {projectID,worker_ids,salaryWay} = this.ctx.data;
		return this.service.allotWKAdmin.allotWorkerInProject({projectID,worker_ids});
	}
	/**
	 * 2020-11-19
	 * 修改工人状态
	 */
	async updateWorkerStatus(){
		let {status,wkID,token} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.allotWKAdmin.updateWorkerStatus({status,wkID});
		}
	}
	/**
	 * 2020-11-20
	 * 删除项目里面的员工
	 */
	async deleteProjectInWorker(){
		let {token,projectID,wkID} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.allotWKAdmin.deleteProjectInWorker({projectID,wkID});
		}
	}
	/**
	 * 2020-11-21
	 * 选择其他项目的员工
	 */
	async selectOtherProjectWorker(){
		let {token,projectID,IDandSalaryWay} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.allotWKAdmin.selectOtherProjectWorker({projectID,IDandSalaryWay});
		}
	}
	/**
	 * 2020-09-23
	 * 获取用户信息，批量获取
	 */
	async getUserData(){
		let {token} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.allotWKAdmin.getUserData();
		}
	}
	/**
	 * 获取项目员工ID
	 * 2020-11-19
	 */
	async getProjectWKID(){
		let {projectID,token} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.allotWKAdmin.getProjectWKID({projectID});
		}
	}
	/**
	 * 根据员工状态和ID获取信息
	 * 2021-3-28
	 */
	async getUserDataByStatus(){
		let {token,workStatus,wkID} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.allotWKAdmin.getUserDataByStatus(workStatus,wkID);
		}
	}
	
	/**
	 * 根据员工所属公司ID获取 员工信息
	 * 2021-6-9
	 */
	async getUserDataByCompanyID(){
		let {token,workStatus,pm_company} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.allotWKAdmin.getUserDataByCompanyID(workStatus,pm_company);
		}
	}
}